%% importFlowHeartRate
%
% Attempts to scrape heart rate information from dicom metadata
%
%% Synatax
%
% [Flow] = importFlowHeartRate(filePathFlow, Flow);
%
% [Flow] = importFlowHeartRate('C:\Path\To\dicom.dcm', Flow);
%
%% Description
%
% This function attempts to import a dicom's metadata and scrape the heart
% rate field. Failure will result in no modifiction to the Flow object
%
%% Arguments
%
%* filePathFlow                    - char: Path to the dicom file of
%                                          interest
%
%* Flow                            - struct: Data structure containing
%                                            information from the analysis 
%                                            of LVOT or ascending aorta flow 
%                                            data
%
%% Returns
%
%* Flow                            - struct: Data structure containing
%                                            information from the analysis 
%                                            of LVOT or ascending aorta flow 
%                                            data, including new filed Flow.HR
%                                            containing heart rate
%                                            information
%                                   
%% Function Side Effects
%
%* The Flow data structure will have an additional field Flow.HR containing
%  heart rate information if scraping is successful
%
%* A warning will be thrown if the heart rate field is not found in the
%  metadata
%
%% Exceptions
%
%* An exception will be thrown if Flow is not type struct
%
%* An exception will be thrown if filePathFlow is not a valid file path
%
%* An exception will be thrown if filePathFlow does not point to a dicom
%  file
%
%% See Also
% dicomAnalysis
% impedanceOnlyFunc
% getFlowHeartRate
%

function [Flow] = importFlowHeartRate(filePathFlow,Flow)

if isa(Flow, 'struct') == 0
    flowType = class(Flow);
    error('importFlowHeartRate:invalidFlowType', ...
          'Error: \nFlow data structure is type %s and should be type struct', flowType);
end

% Validates path points to a file
if exist(filePathFlow, 'file') ~= 2
    error('importFlowHeartRate:invalidDirectoryPath',...
          'Error: \nunable to find file matching fully qualified path %s', filePathFlow);
end

% Validates path points to a dicom file
[pathStr,fileName,ext] = fileparts(filePathFlow);

assert(strcmp('.dcm', ext) || strcmp('.dicom', ext), ...
       'importFlowHeartRate:nonDicomFile', ...
       'The file at path %s is not a dicom file (has extension %s)', filePathFlow, ext);
   
% Import dicom metadata and attempt to extract heart rate field 
   
dicomMeta = dicominfo(filePathFlow);

try 
    
    heartRate = getfield(dicomMeta, 'HeartRate');
    
    Flow.HR = heartRate;
    
catch ME
    
    warning('importFlowHeartRate:unableToFindHeartRateField', ...
            'Unable to find "HeartRate" field in metadata. Manual input of data required');


end

